<template>
  <div class="backStack-issue-wrap informationCreate">
    <div v-for="(item, index) in formList" :key="index" class="mapStatis">
      <TitleCard
        v-if="item.title"
        :title="item.title"
        :showMore="showMore"
        :type="'firstCard'"
      ></TitleCard>
      <div v-else class="allTown">
        <span>全市数据标题：</span>
        <a-input placeholder="请输入标题" v-model="title"/>
      </div>
      <From
        :ref="`formRef${index}`"
        layoutType="grid"
        :isErrorMessage="false"
        :optionList="item.formRows"
        :formRules="item.formRules"
        :defaultModel="item.defaultModel"
      >
      </From>
    </div>
    <div class="backStack-issue-footer-wrap">
      <div class="submit-wrap">
        <a-button class="submit-audit" @click="toAudit">保存并发布</a-button>
      </div>
    </div>
  </div>
</template>

<script>
import From from '../../components/formTable/form.vue'
import mixinsBackStack from '@/mixins/backStack'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinFakeData from '@/mixins/makeFakerData'
import { formRules, formRows, formList } from './configs/formOptions.js'
import { addMapStatistics } from '@/api/mapStatistics'
import { cloneDeep } from 'lodash'

export default {
  mixins: [mixinsBackStack, mixinConfigSetting, mixinFakeData],
  data() {
    return {
      loading: true,
      formRows, // 表单item配置
      formRules, // 表单校验规则配置
      formList,
      defaultModel: {},
      isRead: false,
      copyList: [],
      title: '',
      cityList: ['南海区', '三水区', '禅城区', '顺德区', '高明区']
    }
  },
  components: {
    From
  },
  created() {
    this.copyList = cloneDeep(this.formRows)
  },
  methods: {
    async toAudit() {
      if (!this.title) {
        this.$message.warning('标题未填写!')
        return
      }
      for (let i = 0; i < this.formList.length; i++) {  //验证每个表单
        const ref = 'formRef' + i
        const v = this.$refs[ref][0].validatedForm()
        if (!v) return
      }
      try {
        const mapIpData = this.formList.reduce((pre, cur, index) => {  //取每个表单value
          const ref = 'formRef' + index
          const v = this.$refs[ref][0].formModel
          pre.push(v)
          return pre
        }, [])
        mapIpData.forEach((item, index) => {  //根据返回数据顺序添加请求参数值
          if (index === 0) {
            item.cityTilte = this.title
            item.isCity = '1'
          } else {
            item.cityTilte = this.cityList[index - 1]
            item.isCity = '0'
          }
        })
        const { msg, code } = await addMapStatistics({ mapIpData })
        this.$apiStatusHandler(code, msg)
        this.$router.push({ name: 'systemMapStatistics' })
      } catch (error) {
        console.log(error)
        this.$apiStatusHandler(99, error)
      }
    }
  }
}
</script>
